﻿using PlanesManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PlanesManager.Controllers
{
    [Authorize(Roles="Admin")]
    public class AdminController : Controller
    {
        private PlanesManagerEntities Data = new PlanesManagerEntities();
        //
        // GET: /Admin/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Planes()
        {
            return View(Data.Aircraft.ToList());
        }

        public ActionResult AddPlane()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddPlane(Aircraft model)
        {
            if (ModelState.IsValid)
            {
                Data.Aircraft.Add(new Aircraft() { ID = Guid.NewGuid().ToString(), Name = model.Name, Lotation = model.Lotation });
                Data.SaveChanges();
                return RedirectToAction("Planes",Data.Aircraft.ToList());
            }
            return View(model);
        }

        public ActionResult Airports(){
            return View(Data.Airports.ToList());
        }

        public ActionResult AddAirport(){
            return View();
        }

        [HttpPost]   
        public ActionResult AddAirport(Airports model)
        {
            if (ModelState.IsValid)
            {
                Data.Airports.Add(new Airports() { ID = Guid.NewGuid().ToString(), Name = model.Name, City = model.City });
                Data.SaveChanges();
                return RedirectToAction("Airports", Data.Airports.ToList());
            }
            return View(model);
        }
        public ActionResult Routes()
        {
            return View(Data.Routes.ToList());
        }

        public ActionResult AddRoute()
        {
            ViewBag.Airports = Data.Airports.ToList();
            return View();
        }

        [HttpPost]
        public ActionResult AddRoute(Routes model)
        {
            if (ModelState.IsValid)
            {
                var source = Data.Airports.Find(model.Airports.ID);
                var destination = Data.Airports.Find(model.Airports1.ID);

                Data.Routes.Add(new Routes() { ID = Guid.NewGuid().ToString(), Name = source.City + ", " + source.Name + " - " + destination.City + ", " + destination.Name, Airports = source, Airports1 = destination, });
                Data.SaveChanges();
                return RedirectToAction("Routes", Data.Routes.ToList());
            }
            ViewBag.Airports = Data.Airports.ToList();
            return View(model);
        }

        public ActionResult Flights()
        {
            return View(Data.Flights.ToList());
        }

        public ActionResult AddFlight()
        {
            ViewBag.Routes = Data.Routes.ToList();
            ViewBag.Planes = Data.Aircraft.ToList();
            return View();
        }

        [HttpPost]
        public ActionResult AddFlight(Flights model)
        {
            if (ModelState.IsValid)
            {
                var plane = Data.Aircraft.Find(model.Aircraft.ID);
                var route = Data.Routes.Find(model.Routes.ID);

                Data.Flights.Add(new Flights() { ID = Guid.NewGuid().ToString() , Name = model.Name, Aircraft = plane , Routes = route, Lotation = plane.Lotation, Departure_Date = model.Departure_Date, Arrival_Date = model.Arrival_Date });
                Data.SaveChanges();
                return View("Flights", Data.Flights.ToList());
            }
            ViewBag.Routes = Data.Routes.ToList();
            ViewBag.Planes = Data.Aircraft.ToList();
            return View(model);
        }
	}
}